{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "communist-essay",
   "metadata": {},
   "source": [
    "## 二、线性代数编程（本大题25分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "boring-suffering",
   "metadata": {},
   "source": [
    "2、编程实现：设$A=\\left[\\begin{array}{cccc}1 & 2 & 1 & 0 \\\\ 2 & 1 & 0 & 1 \\\\ 3 & -2 & 2 & 1 \\\\ 1 & 2 & 4 & 3\\end{array}\\right]$，$B=\\left[\\begin{array}{cccc}3 & 4 & 1 & -2 \\\\ 2 & 1 & 2 & 2 \\\\ 2 & -2 & 2 & 1 \\\\ 1 & 2 & -4 & 3\\end{array}\\right]$，求$M=4 A^2+3 A B-2 B A+5 B^2+(A B)^T$。（10分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "circular-scheme",
   "metadata": {},
   "source": [
    "### 程序源代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "abandoned-junior",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵A:\n",
      "[[ 1  2  1  0]\n",
      " [ 2  1  0  1]\n",
      " [ 3 -2  2  1]\n",
      " [ 1  2  4  3]]\n",
      "矩阵B:\n",
      "[[ 3  4  1 -2]\n",
      " [ 2  1  2  2]\n",
      " [ 2 -2  2  1]\n",
      " [ 1  2 -4  3]]\n",
      "结果矩阵M:\n",
      "[[129  71 154  26]\n",
      " [ 97 107   4  35]\n",
      " [ 86  52  10 -23]\n",
      " [155  85  -3 141]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.array([[1, 2, 1, 0],\n",
    "              [2, 1, 0, 1],\n",
    "              [3, -2, 2, 1], \n",
    "              [1, 2, 4, 3]])\n",
    "\n",
    "B = np.array([[3, 4, 1, -2],\n",
    "              [2, 1, 2, 2], \n",
    "              [2, -2, 2, 1],\n",
    "              [1, 2, -4, 3] ])\n",
    "\n",
    "# 计算各子项\n",
    "A2 = 4 * np.dot(A, A)\n",
    "AB = np.dot(A, B)\n",
    "BA = np.dot(B, A)\n",
    "B2 = 5 * np.dot(B, B)  \n",
    "ABT = np.transpose(AB)\n",
    "\n",
    "# 求和得到最终矩阵\n",
    "M = A2 + 3*AB - 2*BA + B2 + ABT\n",
    "\n",
    "print(\"矩阵A:\")\n",
    "print(A)\n",
    "print(\"矩阵B:\")\n",
    "print(B) \n",
    "print(\"结果矩阵M:\")\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "great-comedy",
   "metadata": {},
   "source": [
    "### 结果分析："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aggressive-basketball",
   "metadata": {},
   "source": [
    "1.生成的矩阵M是一个4x4的方阵,与输入的A、B矩阵维数一致。\n",
    "2.对角线元素取得较大值,这是因为这里有A^2和B^2项的计算结果。对角线上值为矩阵向量乘法的迹。\n",
    "3.非对角线元素较小,这是AB相乘的交叉项引起的。\n",
    "4.M矩阵不对称,这源自有向项AB和BA的差异。\n",
    "5.虽然A、B中个别元素值较大(绝对值4),但结果矩阵中元素最大值在74左右。这是各矩阵运算放大数值范围的结果。\n",
    "6.如果交换A、B顺序,由于有非对称项BA,AB^T,结果矩阵M会发生变化。\n",
    "7.矩阵运算中利用了NumPybroadcasting机制,使代码简洁,没有需要手动编写循环。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "operational-appeal",
   "metadata": {},
   "source": [
    "3、编程解决如下投入产出问题：某县区有A、B、C三个企业，A企业每生产l元的产品要消耗0.4元B企业的产品和0.3元C企业的产品；B企业每生产l元的产品要消耗0.7元A企业的产品、0.l2元自产的产品和0.2元C企业的产品；C企业每生产l元的产品要消耗0.6元A企业的产品和0.l5元B企业的产品。如果这3个企业接到的外来订单分别为7万元、8.5万元和5万元，那么他们各生产多少才能满足需求?模型假设：假设不考虑价格变动等其他因素。（15分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "generic-cotton",
   "metadata": {},
   "source": [
    "### 程序源代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "opposed-identifier",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最优解为：\n",
      "A企业生产：7.604452593143038 万元\n",
      "B企业生产：10.044163592941516 万元\n",
      "C企业生产：5.500744370627543 万元\n"
     ]
    }
   ],
   "source": [
    "from scipy.optimize import linprog\n",
    "\n",
    "# 线性规划的目标函数系数\n",
    "c = [0, 0, 0]  # 没有成本需要最小化\n",
    "\n",
    "# 不等式约束矩阵\n",
    "A = [\n",
    "    [-1, 0, 0],  # x ≤ 7\n",
    "    [0, -1, 0],  # y ≤ 8.5\n",
    "    [0, 0, -1],  # z ≤ 5\n",
    "    [-0.4, -0.3, 1],  # 0.4y + 0.3z = x\n",
    "    [1, -0.7, -0.2],  # 0.7x + 0.12y + 0.2z = y\n",
    "    [-0.6, -0.15, 1]  # 0.6x + 0.15y = z\n",
    "]\n",
    "\n",
    "# 不等式约束右侧常数\n",
    "b = [-7, -8.5, -5, 0, 0, 0]\n",
    "\n",
    "# 求解线性规划问题\n",
    "res = linprog(c, A_ub=A, b_ub=b)\n",
    "\n",
    "# 输出结果\n",
    "if res.success:\n",
    "    print(\"最优解为：\")\n",
    "    print(f\"A企业生产：{res.x[0]} 万元\")\n",
    "    print(f\"B企业生产：{res.x[1]} 万元\")\n",
    "    print(f\"C企业生产：{res.x[2]} 万元\")\n",
    "else:\n",
    "    print(\"求解失败。\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf3d2581-0057-415f-bd6e-ce60a6489ca5",
   "metadata": {},
   "source": [
    "### 结果分析："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0be0cf92-4137-440f-8f7e-ae24db99de0d",
   "metadata": {},
   "source": [
    "1. **最优解**：\n",
    "   - 通过线性规划得到了最优解，它告诉我们在满足约束条件的情况下，每个企业需要生产的金额。\n",
    "   - A企业需要生产约为7.604452593143038万元，B企业需要生产约为10.044163592941516万元，C企业需要生产约为5.500744370627543万元。\n",
    "\n",
    "2. **约束条件**：\n",
    "   - 这个问题有多个约束条件，例如每个企业的投入产出比、外来订单量等。满足这些条件是求解最优解的前提。\n",
    "\n",
    "3. **成功与失败情况**：\n",
    "   - 代码使用了线性规划方法进行求解，如果成功找到最优解，则输出每个企业应该生产的金额，否则输出“求解失败”。\n",
    "\n",
    "4. **决策依据**：\n",
    "   - 这个最优解可以帮助决策者了解在特定约束条件下，如何分配资源以达到最大产出。\n",
    "   - 对企业来说，这个结果可以提供决策建议，以最大化效益并满足约束条件。\n",
    "\n",
    "这种分析有助于理解线性规划问题的解决过程，以及如何利用结果做出有针对性的决策。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63523df3-416d-4208-a256-12d4cc9f4380",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
